/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package CapaPresentacion;

import CapaLogica.Gestor;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;

/**
 *
 * @author Elayner
 */ 
public class PantallaArtistaAgregar extends javax.swing.JFrame {
    
    private static Gestor gestor=new Gestor();
    private static ArrayList <TreeMap> listaMaestros; 
    private static TreeMap datosMaestros;
    private Date fechaActual=new Date();
    //Variables del form
    private int idMaestro;
    private int id;
    private String nombre;
    private String alias;
    private String nacionalidad;
    private String ciudad;
    private String fec_nac;
    private String fec_fall;
    private int pindex_maestro;
    private String indTipo;
        
        
    public PantallaArtistaAgregar() throws Exception{
        initComponents();
        iniListMaestros();
        iniListEscuelas();
        fec_naci_art.setMaxSelectableDate(fechaActual);
    }
    
    public void iniListMaestros() throws Exception{        
        listaMaestros = gestor.buscarArtistaPorNombre("");

        for (int i=0; i<listaMaestros.size(); i++) {
            datosMaestros =(TreeMap)listaMaestros.get(i);
            maestro_art.addItem(" "+datosMaestros.get("id")+"-"+datosMaestros.get("nombre"));
        }
    }
    
    public void iniListEscuelas() throws Exception{        
        String[]listaEscuelas=helperUI.getListaEscuelas("");

        for (int i=0; i<listaEscuelas.length; i++) {
            escuela_art.addItem(listaEscuelas[i]);
        }
    }
    
    public boolean validarPantalla(){
        boolean result=true;
        if(id<=0)
            result=false;
        if(nom_art.getText().length()<=0)
            result=false;
        if(nac_art.getText().length()<=0)
            result=false;
        if(ciud_art.getText().length()<=0)
            result=false;
        if(fec_naci_art==null)
            result=false;
        return result;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        ced_Art = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        nom_art = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        alias_art = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        nac_art = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        fec_naci_art = new com.toedter.calendar.JDateChooser();
        jLabel6 = new javax.swing.JLabel();
        fec_fall_art = new com.toedter.calendar.JDateChooser();
        indTipoArt = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        ciud_art = new javax.swing.JTextField();
        btn_aceptar_art = new javax.swing.JButton();
        btn_cancelar_art = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        escuela_art = new javax.swing.JComboBox();
        jLabel10 = new javax.swing.JLabel();
        maestro_art = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Registrar Artista");

        jLabel1.setText("Cedula:");

        jLabel2.setText("Nombre Completo:");

        nom_art.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nom_artActionPerformed(evt);
            }
        });

        jLabel3.setText("Alias:");

        jLabel4.setText("Nacionalidad:");

        jLabel5.setText("Fecha de nacimiento");

        fec_naci_art.setMinSelectableDate(new java.util.Date(-62135744299000L));
        fec_naci_art.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                fec_naci_artPropertyChange(evt);
            }
        });

        jLabel6.setText("Fecha de fallecimiento:");

        fec_fall_art.setMaxSelectableDate(new java.util.Date(253370790116000L));

        indTipoArt.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "F", "R", "NR" }));

        jLabel7.setText("Tipo artista:");

        jLabel8.setText("Ciudad:");

        btn_aceptar_art.setText("Aceptar");
        btn_aceptar_art.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_aceptar_artActionPerformed(evt);
            }
        });

        btn_cancelar_art.setText("Cancelar");
        btn_cancelar_art.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_cancelar_artActionPerformed(evt);
            }
        });

        jLabel9.setText("Escuela:");

        escuela_art.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Ninguna" }));

        jLabel10.setText("Maestro");

        maestro_art.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Ninguno" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(26, 26, 26)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(18, 18, 18)
                                .addComponent(ced_Art, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(jLabel9))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel7))
                                .addGap(18, 18, 18)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(indTipoArt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(fec_fall_art, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel3)
                                        .addGap(18, 18, 18)
                                        .addComponent(alias_art, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel4)
                                        .addGap(18, 18, 18)
                                        .addComponent(nac_art, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addGap(18, 18, 18)
                                        .addComponent(nom_art, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel5)
                                        .addGap(18, 18, 18)
                                        .addComponent(fec_naci_art, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jLabel8)
                                        .addGap(18, 18, 18)
                                        .addComponent(ciud_art, javax.swing.GroupLayout.PREFERRED_SIZE, 129, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 95, Short.MAX_VALUE)
                                .addComponent(jLabel10)
                                .addGap(1, 1, 1))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(162, 162, 162)
                        .addComponent(btn_aceptar_art)
                        .addGap(51, 51, 51)
                        .addComponent(btn_cancelar_art)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(escuela_art, javax.swing.GroupLayout.PREFERRED_SIZE, 144, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(maestro_art, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(40, 40, 40)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(ced_Art, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel9)
                    .addComponent(escuela_art, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(nom_art, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(alias_art, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(nac_art, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(30, 30, 30)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel8)
                            .addComponent(ciud_art, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5)
                            .addComponent(fec_naci_art, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel6)
                            .addComponent(fec_fall_art, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(indTipoArt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel10)
                            .addComponent(maestro_art, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btn_aceptar_art)
                    .addComponent(btn_cancelar_art))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btn_aceptar_artActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_aceptar_artActionPerformed
           
           idMaestro=0;
           if(idMaestro>0)
            id=Integer.parseInt(ced_Art.getText());
        
        if(validarPantalla()){
           nombre=nom_art.getText();
           alias=alias_art.getText();
           nacionalidad=nac_art.getText();
           ciudad=ciud_art.getText();
           fec_nac=helperUI.getDateFormatConv(fec_naci_art.getDate());
           fec_fall=fec_fall_art.getDate()!=null? helperUI.getDateFormatConv(fec_fall_art.getDate()):"01/01/3000";
           pindex_maestro=maestro_art.getSelectedIndex()-1;
           indTipo=(String)indTipoArt.getSelectedItem();
            
            if(pindex_maestro>=0)
                idMaestro=(int)listaMaestros.get(pindex_maestro).get("id");

            String nomEscuela=(String)escuela_art.getSelectedItem();

            try {
                if(gestor.registrarArtista(id, nombre, alias, nacionalidad, ciudad, fec_nac, fec_fall, indTipo, idMaestro,nomEscuela)){
                    this.setVisible(false);
                }
            } catch (Exception ex) {
                Logger.getLogger(PantallaArtistaAgregar.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(this,(String) ex.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);	
            }
        }else{
             JOptionPane.showMessageDialog(this,(String) "Debe de llenar los campos requeridos","Error",JOptionPane.ERROR_MESSAGE);	
        }
        
        
        
    }//GEN-LAST:event_btn_aceptar_artActionPerformed

    private void btn_cancelar_artActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_cancelar_artActionPerformed
       this.setVisible(false);
    }//GEN-LAST:event_btn_cancelar_artActionPerformed

    private void nom_artActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nom_artActionPerformed

    }//GEN-LAST:event_nom_artActionPerformed

    private void fec_naci_artPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_fec_naci_artPropertyChange
        fec_fall_art.setMinSelectableDate(fec_naci_art.getDate());
        fec_fall_art.setMaxSelectableDate(fechaActual);
    }//GEN-LAST:event_fec_naci_artPropertyChange


    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PantallaArtistaAgregar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PantallaArtistaAgregar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PantallaArtistaAgregar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PantallaArtistaAgregar.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                try {
                    new PantallaArtistaAgregar().setVisible(true);
                } catch (Exception ex) {
                    Logger.getLogger(PantallaArtistaAgregar.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField alias_art;
    private javax.swing.JButton btn_aceptar_art;
    private javax.swing.JButton btn_cancelar_art;
    private javax.swing.JTextField ced_Art;
    private javax.swing.JTextField ciud_art;
    private javax.swing.JComboBox escuela_art;
    private com.toedter.calendar.JDateChooser fec_fall_art;
    private com.toedter.calendar.JDateChooser fec_naci_art;
    private javax.swing.JComboBox indTipoArt;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JComboBox maestro_art;
    private javax.swing.JTextField nac_art;
    private javax.swing.JTextField nom_art;
    // End of variables declaration//GEN-END:variables

}


